import router from './router';
import nprogress from 'nprogress';
import 'nprogress/nprogress.css';

nprogress.configure({ showSpinner: false });

// 增加三方登陆 update by 芋艿
const whiteList = ['/login', '/register'];

router.beforeEach((to, from, next) => {
  nprogress.start();
  if (getAccessToken()) {
    next();
  } else {
    if (whiteList.indexOf(to.path) !== -1) {
      // 在免登录白名单，直接进入
      next();
    } else {
      const redirect = encodeURIComponent(to.fullPath); // 编码 URI，保证参数跳转回去后，可以继续带上
      next(`/login?redirect=${redirect}`); // 否则全部重定向到登录页
    }
  }
  nprogress.done();
});

router.afterEach(() => {
  nprogress.done();
});
